@extends('admin.layouts.app')

@section('right-content')
    {{-- <meta name="csrf-token" content="{{ csrf_token() }}"> --}}
    <fieldset class="layui-elem-field layui-field-title">
        <legend>
        <span class="layui-breadcrumb">
            <a href="javascript:;">消息管理</a>
            <a><cite>公告列表</cite></a>
        </span>
        </legend>
    </fieldset>

    <table id="dateTable" class="layui-table">
        <thead>
        <tr>
            <th><input type="checkbox" class="my-checkbox" /></th>
            <th>序号</th>
            <th>标题</th>
            <th>内容</th>
            <th>收件人</th>
            <th>发布人</th>
            <th>发布时间</th>
            {{-- <th>状态</th> --}}
            <th>操作</th>
        </tr>
        </thead>
        <tbody>
        @foreach($emails as $key => $value)
            <tr>
                <td><input type="checkbox" class="my-checkbox" data-id="{{ $value->id }}" /></td>
                <td>{{ $key + 1 }}</td>
                <td>{{ $value->title }}</td>
                <td>{{ $value->content }}</td>
                <td>@switch($value->receiver_account_id) @case(0)所有人 @break @case(-1) 贵宾玩家 @break @case(-2)休闲玩家 @break @default @endswitch</td>
                <td>{{ $value->sender_account_name }}</td>
                <td>{{ $value->published_at }}</td>
                {{-- <td>@if($value->enable == 1) 上线 @else 下线 @endif</td> --}}
                <td>
                    <button class="layui-btn layui-btn-small layui-btn-normal edit" data-url="{{ route('emails.edit', ['id' => $value->id]) }}">编辑</button>
                    <button class="layui-btn layui-btn-small layui-btn-danger delete" data-url="{{ route('emails.destroy', ['id' => $value->id]) }}">删除</button>
                </td>
            </tr>
        @endforeach
        </tbody>
    </table>

@endsection

@section('my-js')
<script type="text/javascript" src="http://cdn.datatables.net/1.10.13/js/jquery.dataTables.min.js"></script>
<script type="text/javascript" src="{{ asset('admin/js/table-tool.js') }}"></script>
<script type="text/javascript">
    layui.use('layer', function() {
        var $ = layui.jquery,element = layui.element,layer = layui.layer;

        // 初始化表格
        $('#dateTable').DataTable({
            "dom": '<"top"f>rt<"bottom"lp><"clear">',
            "lengthChange": true,
            "autoWidth": true,                     // 自适应宽度
            "stateSave": true,                      // 刷新后保存页数
            'ordering' : false,
            // "order": [[ 1, "desc" ]],               // 排序
            "searching": true,                     // 本地搜索
            "info": true,                           // 控制是否显示表格左下角的信息
            "stripeClasses": ["odd", "even"],       // 为奇偶行加上样式，兼容不支持CSS伪类的场合
            "columnDefs": [{                      // 指定列不参与排序
                'width' : '20%',
                "targets": [3]                   // 对应你的表格的列数
            }],
            "pagingType": "simple_numbers",         // 分页样式 simple,simple_numbers,full,full_numbers
            "language": {                           // 国际化
                "url":"{{ asset('admin/language.json') }}"
            },
            initComplete: function() {
                $('.top').append('<span class="fl"><a class="layui-btn btn-add btn-default" id="btn-add-article" href="{{ route('emails.create') }}">新增邮件</a><a class="layui-btn layui-btn-danger radius btn-delect" id="btn-delete-all">批量删除</a></span>');
            }
        });

        // 例:获取ids
        $(document).on('click','#btn-delete-all', function(){
            // getIds(table对象,获取input为id的属性)
            var list = getIds($('#dateTable'),'data-id');
            if(list == null || list == ''){
                layer.msg('未选择');
            }else{
                // layer.msg(list);
                layer.confirm('is not?', {icon: 3, title:'提示', content:'您确定要删除吗？'}, function(index){
                    layer.close(index);
                    $.post("{{ route('emails.destroyList') }}", {ids : list}, function(data) {
                        console.log(data);
                        if (data.status == 0) {
                            top.layer.msg(data.msg, {icon: 1, time: 2000});
                            window.location.href = data.url;
                        } else {
                            top.layer.msg(data.msg, {icon: 2, time: 2000});
                            window.location.reload();
                        }
                    },'json');
                });
            }
        });

        $(document).on('click','.edit', function(){
            var url = $(this).attr('data-url');
            window.location.href = url;
        });

        $(document).on('click','.delete', function(){
            var url = $(this).attr('data-url');
            commonAjaxRequest(url, 'DELETE')
        });

    })

</script>
@endsection